<?php
use yii\helpers\Url;

$web = Url::base();
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$institution_code = \app\models\Mall::getCurrentInstitutionCode();
$this->title = $site_name.'->地址管理';
?>
<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall/user-address.css"/>
<style type="text/css">
    .my-information .shipping-address .address .default{
        background: url(<?= $web ?>/images/mall/elect-background2.png) no-repeat;
    }
</style>
<script type="application/javascript">

    function update(addressId) {
        var data = {};
        data.address_id = addressId;
        requestHtml("user-address-part-view", data, false, function (responsehtml) {
            $("#detail_part_view").html(responsehtml);
        });
    }
    function add() {
        requestHtml("user-address-part-view", null, false, function (responsehtml) {
            $("#detail_part_view").html(responsehtml);
        });
    }

    function save() {
        var address_id = $("#address_id").val();
        var name = $("#name").val();
        var phone = $("#phone").val();
        var province_name = $("#province_name").find("option:selected").text();
        var city_name = $("#city_name").find("option:selected").text();
        var region_name = $("#region_name").find("option:selected").text();
        var address = $("#address").val();
        var remark = $("#remark").val();

        if (name == null || name == '') {
            alert('收货姓名不能为空！');
            return;
        }
        if (phone == null || phone == '') {
            alert('手机号码不能为空！');
            return;
        }
        if (province_name == null || province_name == '' || province_name == '省') {
            alert('收货省不能为空！');
            return;
        }
        if (city_name == null || city_name == '' || city_name == '市') {
            alert('收货市不能为空！');
            return;
        }
        if (region_name == null || region_name == '' || region_name == '区') {
            alert('收货区不能为空！');
            return;
        }
        if (address == null || address == '') {
            alert('详细街道不能为空！');
            return;
        }
        var data = {};
        data.address_id = address_id;
        data.name = name;
        data.phone = phone;
        data.province_name = province_name;
        data.city_name = city_name;
        data.region_name = region_name;
        data.address = address;
        data.remark = remark;

        requestJson("save-user-address-handler", data, true, function (responseData) {
            window.location.href = "user-address?institution_code=<?= $institution_code ?>";
        });

    }

    function setDefault(address_id) {
        var data = {};
        data.address_id = address_id;
        requestJson("set-default-address-handler", data, true, function (responseData) {

        });
    }

    function deleteData(address_id) {
        var data = {};
        data.address_id = address_id;
        requestJson("delete-address-handler", data, true, function (responseData) {
            window.location.href = "user-address?institution_code=<?= $institution_code ?>";
        });
    }

</script>

<div class="my-information">
    <div class="menu fl">
        <ul>
            <li><a href="user-information?institution_code=<?= $institution_code ?>">个人信息</a></li>
            <li><a href="user-card?institution_code=<?= $institution_code ?>">我的卡卷</a></li>
            <li><a href="user-present?institution_code=<?= $institution_code ?>"><?= $integral_unit ?>转赠</a></li>
            <li><a href="order-list?institution_code=<?= $institution_code ?>">我的订单</a></li>
            <li><a href="user-account-record?institution_code=<?= $institution_code ?>">我的<?= $integral_unit ?></a></li>
            <li><a href="javascript:void(0)" class="cur">收货地址</a></li>
        </ul>
    </div>
    <div class="data fl">
        <div class="title">
            <p>收货地址</p>
            <input class="newly" type="button" value="新增收货地址" onclick="add()"/>
        </div>
        <div class="shipping-address">
            <div class="address fl">
                <?php
                if ($address_list != null) {
                    for ($i = 0; $i < count($address_list); $i++) {
                        ?>
                        <div class="default"
                             style="<?php if ($address_list[$i]['is_default'] == 1) echo "background:url(" . $web . "/images/mall/elect-background1.png);background-size:100% 100%;background-repeat:no-repeat"; else echo "background-size:100% 100%"; ?>">
                            <p style="<?php if ($address_list[$i]['is_default'] == 1) echo "color:#666666"; ?>"><span><?= $address_list[$i]['name'] ?></span>收</p>
                            <p style="<?php if ($address_list[$i]['is_default'] == 1) echo "color:#666666"; ?>"><?= $address_list[$i]['province_name'] . ' ' . $address_list[$i]['city_name'] . ' ' . $address_list[$i]['region_name'] . ' ' . $address_list[$i]['address'] ?></p>
                            <div>
                                <ul>
                                    <li class="phone"
                                        style="<?php if ($address_list[$i]['is_default'] == 1) echo "color:#666666"; ?>"><?= $address_list[$i]['phone'] ?></li>
                                    <li><input class="give" type="button"
                                               onclick="setDefault('<?= $address_list[$i]['id'] ?>')"
                                               value="设为默认地址"/></li>
                                    <li><input class="alter" type="button"
                                               onclick="update('<?= $address_list[$i]['id'] ?>')"
                                               value="修改"/></li>
                                    <li><input type="button" onclick="deleteData('<?= $address_list[$i]['id'] ?>')"
                                               value="删除"/></li>
                                </ul>
                            </div>
                        </div>
                        <?php
                    }
                }
                ?>
            </div>
            <div class="nouveau fl" id="detail_part_view">

            </div>
            <script type="text/javascript">
                $(function() {
                    $(".newly").click(function() {
                        $(".nouveau").addClass("animated fadeInRight").show();
                        setTimeout(function() {
                            $('.nouveau').removeClass('animated fadeInRight');
                        }, 1000);
                        $(".default").css({
                            "background": "url(<?= $web ?>/images/mall/elect-background2.png)",
                            "background-size": "100% 100%",
                            "background-repeat": "no-repeat"
                        });
                        $(".address").animate({
                            "width": "400px"
                        }, 300);
                        $(this).parent().siblings("li.phone").css("color", "#666666");
                        $(this).parent().parent().parent().siblings("p").css("color", "#666666");
                        $(this).parent("li").parent("ul").parent("div").parent(".default").siblings(".default").children("p").css("color", "#d1d1d1");
                        $(this).parent("li").parent("ul").parent("div").parent(".default").siblings(".default").children("div").children("ul").children("li.phone").css("color", "#D1D1D1");
                        $(".default>p,.default>div ul li.phone").css("color", "#d1d1d1");
                    });
                    $(".alter").click(function() {
                        $(".nouveau").addClass("animated fadeInRight").show();
                        setTimeout(function() {
                            $('.nouveau').removeClass('animated fadeInRight');
                        }, 1000);
                        $(".default").css({
                            "background": "url(<?= $web ?>/images/mall/elect-background2.png)",
                            "background-size": "100% 100%",
                            "background-repeat": "no-repeat"
                        });
                        $(".address").animate({
                            "width": "400px"
                        }, 300);
                        $(this).parent().siblings("li.phone").css("color", "#666666");
                        $(this).parent().parent().parent().siblings("p").css("color", "#666666");
                        $(this).parent("li").parent("ul").parent("div").parent(".default").siblings(".default").children("p").css("color", "#d1d1d1");
                        $(this).parent("li").parent("ul").parent("div").parent(".default").siblings(".default").children("div").children("ul").children("li.phone").css("color", "#D1D1D1");
                    });
                    $(".give").click(function() {
                        $(".nouveau").addClass("animated fadeOutRight");
                        setTimeout(function() {
                        $('.nouveau').removeClass('animated fadeOutRight').hide();
                    }, 1000);

                        $(this).parent("li").parent("ul").parent("div").parent(".default").css({
                            "background": "url(<?= $web ?>/images/mall/elect-background1.png)",
                            "background-size": "100% 100%",
                            "background-repeat": "no-repeat"
                        });
                        $(this).parent("li").parent("ul").parent("div").parent(".default").siblings().css({
                            "background": "url(<?= $web ?>/images/mall/elect-background2.png)",
                            "background-size": "100% 100%",
                            "background-repeat": "no-repeat"
                        });
                        $(".address").animate({
                            "width": "950px"
                        }, 700);
                        $(this).parent().siblings("li.phone").css("color", "#666666");
                        $(this).parent().parent().parent().siblings("p").css("color", "#666666");
                        $(this).parent("li").parent("ul").parent("div").parent(".default").siblings(".default").children("p").css("color", "#d1d1d1");
                        $(this).parent("li").parent("ul").parent("div").parent(".default").siblings(".default").children("div").children("ul").children("li.phone").css("color", "#D1D1D1");
                    });
                    $(".default").mouseover(function() {
                        $(this).find("div ul li input").css("display", "block")
                    });
                    $(".default").mouseout(function() {
                        $(this).find("div ul li input").css("display", "none")
                    });
//                    $(".default:first-child").css({"background": "url(/elect-background1.png)","background-size": "100% 100%","background-repeat": "no-repeat"})
//                    $(".default:first-child>p,.default:first-child>div ul li.phone").css("color", "#666666");
                });
            </script>
        </div>
    </div>
</div>